package org.java.test;

import java.io.IOException;
import java.util.Properties;
/**
 * 读取资源文件信息
 */
public class ReaderResourceFile {
	private static Properties pro=new Properties();
	static {
		try {
			//通过类加载器classLoader的getResourceAsStream（）方法获取资源
			ClassLoader cl=ReaderResourceFile.class.getClassLoader();
			pro.load(cl.getResourceAsStream("resource.properties"));
			//pro.load(ReaderResourceFile.class.getResourceAsStream("/resource.properties"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//通过资源文件的key得到相应的value
	public static String getValue(String key) {
		String keyValue ="";
		try {
			if (key != null && !"".equals(key)) {
				keyValue = new String(pro.getProperty(key).getBytes(), "UTF-8");
			}
			if ("".equals(keyValue)) {
				return key;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return keyValue;
	}
	//==================================================================	
	/**
	 * 以下为测试代码
	 */
	public static void main(String[] args) {
		String value=ReaderResourceFile.getValue("name");		
		System.out.println("value====="+value);
	}
}
